<?php

namespace App\Repositories;

use App\Models\Menu;
use App\Exceptions\InvalidException;
use DB;
use Hash;
use App\Models\User;
class UserRepo
{
	public function getUserByPassword($account,$password,$mobile)
	{
		
		$user =  User::where(function($query)use($account,$mobile){
				if($account) {
					$query->where('account',$account);
				}
				if($mobile) {
					$query->where('mobile',$mobile);
				}
		})->first();

		if($user) {
			$user->last_login_at = date('Y-m-d H:i:s',time());
			$user->save();
		}

		return $user;
	}

	public function getUser($id) 
	{
		return User::where('id',$id)->first();
	}
	public function getUserList($where,$page=10)
	{
		return User::where(function($query)use($where){
		})->select('id','status','mark','mobile','created_at','head_img','account','user_name')->simplePaginate($page)->toArray();
	}
	public function register($account,$password='',$head_img='',$user_name)
	{
		return User::create(['account'=>$account,'password'=>Hash::make('password', [
                'rounds' => 12]),'head_img'=>$head_img,'user_name'=>$user_name]);
	}

	public function updateUser($id,$data)
	{
		$user = User::where('id',$id)->first();

		if(!$user) {
			throw new InvalidException("用户不存在");
		}
		$user->mark = $data['mark'];
		$user->save();
	}

	public function bindMobile($id,$mobile)
	{
		$user = User::where('id',$id)->first();

		if(!$user) {
			throw new InvalidException("用户不存在");
		}
		$user->mobile = $mobile;
		$user->save();
	}
}